import router from "@/router";
import { getToken } from "@/utils/auth.js";
import { toast } from "@/utils";

//全局前置守卫
router.beforeEach((to, form, next) => {
  const token = getToken();
  //没有登录强制跳转
  if (!token && to.path != "/login") {
    if (form.path != "/") toast("请先登录", "error");
    return next({ path: "/login" });
  }

  //防止重复登录
  if (token && to.path == "/login") {
    toast("请勿重复登录", "error");
    return next({ path: form.path ? form.path : "/" });
  }

  next();
});
